package com.enjoyor.soa.traffic.frame.support.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.enjoyor.soa.traffic.util.bean.Test.Test;
import com.enjoyor.soa.traffic.util.exception.ManagerException;

public class DomainUtil {

    public static Object merge2Object(Object source, Object target) {
        try {
            BeanUtils.copyProperties(source, target);
            return target;
        } catch (Exception e) {
            throw new ManagerException("类合并异常！");
        }
    }

    public static <T, S> List<T> merge2ListDefault(List<S> sources, Class<T> clazz) {

        try {

            List<T> list = new ArrayList<>();

            if (sources != null && !sources.isEmpty()) {

                for (S source : sources) {

                    T target = clazz.newInstance();

                    BeanUtils.copyProperties(source, target);

                    list.add(target);

                }

            }

            return list;

        } catch (Exception e) {
            throw new ManagerException("类合并异常！");
        }

    }

    @SuppressWarnings("unchecked")
    public static <T, S> List<T> merge2ListJson(List<S> sources, Class<T> clazz) {

        try {

            List<T> list = new ArrayList<>();

            if (sources != null && !sources.isEmpty()) {

                for (S source : sources) {

                    String sourceStr = JSON.toJSONString(source, SerializerFeature.DisableCircularReferenceDetect);

                    Object object = JSON.parseObject(sourceStr, clazz);

                    list.add((T) object);

                }

            }

            return list;

        } catch (Exception e) {
            throw new ManagerException("类合并异常！");
        }

    }

    public static <T> Object merge2ObjectJson(Object source, Class<T> clazz) {

        try {
            Object object = null;
            if (source != null ) {
                String sourceStr = JSON.toJSONString(source, SerializerFeature.DisableCircularReferenceDetect);
                 object = JSON.parseObject(sourceStr, clazz);
            }
            return object;
        } catch (Exception e) {
            throw new ManagerException("类合并异常！");
        }
    }

    // 测试 merge方法
    public static void main(String[] args) {

        Test target0 = new Test();
        target0.setId("id0");
        target0.setCode("code");
        target0.setName("name");
        target0.setDate(new Date());
        target0.setEnabled(Boolean.valueOf(false));
        target0.setStatus(Integer.valueOf(1));

        System.out.println("target0 == " + target0.toString());
        System.out.println();  

        Test source2 = new Test();

        source2.setCode("code2");
        source2.setName("name2");

        source2.setStatus(Integer.valueOf(2));
        System.out.println("source2 == " + source2.toString());

        Test target2 = (Test) merge2Object(target0, source2);
        BeanUtils.copyProperties(target0, source2);
        // System.out.println("target0 == " + target0.toString());
        System.out.println("target2 == " + target2);
        System.out.println();

    }

}
